<?php
/*
Plugin Name: 静态化插件
Version: 2.0
Plugin URL:
Description: 生成全站页面到静态文件。减轻服务器压力.提高访问速度,提高搜索引擎收录质量.
Author: Uncle Liu
Author Email: te3@qq.com
Author URL: http://www.uncleliu.cn/post-158.html
*/

!defined('EMLOG_ROOT') && exit('access deined!');
//发表日志生成HTML
function html_addlog()
{
	global $blogid;
	global $logData;
	if($logData['password'] == '')
	{
		$html_articlefile = file_get_contents(BLOG_URL."?post=".$blogid);
	}else{
		$html_articlefile = "<script language='javascript'>location.href='./?post=".$blogid."'</script>";
	}
	$html_writehtml = fopen("../post-".$blogid.".html","w");
	fwrite($html_writehtml,$html_articlefile);
	fclose($html_writehtml);
	
	global $DB;//更新TAG
	$html_taglist = $DB->query("select * from ".DB_PREFIX."tag where `gid` = \",".$blogid.",\"");
	while($html_tagrow = $DB->fetch_array($html_taglist))
	{
		$html_utftag = $html_tagrow['tagname'];
		$html_utftag = str_replace(" ","+",$html_utftag);
		$html_gbktag = iconv("UTF-8", "GBK", $html_tagrow['tagname']);
		$html_gbktag = str_replace(" ","+",$html_gbktag);
		$html_tagurl = urlencode($html_tagrow['tagname']);
		$html_blogfile = file_get_contents(BLOG_URL."?tag=".$html_tagurl);
		$html_writehtml = @fopen("../tag-".$html_gbktag.".html","w");
		if(@fwrite($html_writehtml,$html_blogfile))
		{
			if(!@file_get_contents(BLOG_URL."tag-".$html_tagurl.".html"))
			{
				$html_writetag = @fopen("../tag-".$html_utftag.".html","w");
				@fwrite($html_writetag,$html_blogfile);
				@fclose($html_writetag);
			}
		}
		@fclose($html_writehtml);
	}
}

addAction('save_log', 'html_addlog');

//增加评论更新HTML
function html_comment()
{
	global $gid;
	$html_articlefile = file_get_contents(BLOG_URL."?post=".$gid);
	$html_articlefile = str_replace("<form action=\"\" method=\"post\">","<form action=\"./?post=".$gid."\" method=\"post\">",$html_articlefile);
	$html_writehtml = fopen("post-".$gid.".html","w");
	@fwrite($html_writehtml,$html_articlefile);
	fclose($html_writehtml);
}

addAction('comment_saved', 'html_comment');

//删除日志删除HTML
function html_dellog()
{
	global $key;
	@unlink("../post-".$key.".html");
}

addAction('del_log', 'html_dellog');

//写入头部触发更新的JS

function html_auto_js()
{
	echo "<script type=\"text/javascript\">
var now = new Date();
var number = now.getSeconds();
document.write ('\<script src=\"./content/plugins/html/auto.php?'+number+'\"\>\</script\>');
</script>";
}

addAction('index_head', 'html_auto_js');

function html_menu()//写入插件导航
{
	echo '<div class="sidebarsubmenu" id="html"><a href="./plugin.php?plugin=html">更新HTML</a></div>';
}

addAction('adm_sidebar_ext', 'html_menu');

?>